package cn.com.headfree.technician.controller;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.TEducateInfo;
import cn.com.headfree.entity.TEmpBaseInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.technician.service.ExportWordUserDataInfoService;
import cn.com.headfree.technician.service.IUserDataInfoService;
import cn.com.headfree.technician.service.TSeniorTechnicianHandleService;
import cn.com.headfree.technician.service.WordExportService;
import cn.com.headfree.util.SysDictionaryUtil;
import cn.com.headfree.vo.*;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 *技能等级申报
 */
@Controller
@RequestMapping(value = "/technician", produces = "application/json;charset=UTF-8")
public class TechnicianController extends ExBaseController {
	private static final Logger LOG = LogManager.getLogger(TechnicianController.class);

	@Autowired
	private IUserDataInfoService userDataInfoService;

	@Autowired
    private TSeniorTechnicianHandleService tSeniorTechnicianHandleService;

	@Autowired
	private ExportWordUserDataInfoService exportWordUserDataInfoService;

	@Autowired
	private WordExportService wordExportService;

	@Autowired
	ResourceLoader resourceLoader;

	/**
	 * 基本情况
	 * @param erpNo
	 * @return
	 */
	@RequestMapping("/getTEmpBaseInfoForSenior")
	@ResponseBody
	public ResultData getTEmpBaseInfoForSenior(String erpNo) {
		ResultData rs = new ResultData();
		try {
			TEmpBaseInfo tEmpBaseInfo = userDataInfoService.getBaseInfoByErpNo(erpNo);
			//文化程度--获取最高学历
			List<TEducateInfo> educateInfo = userDataInfoService.getEducateInfoHighEducByErpNo(erpNo);
			if(tEmpBaseInfo != null){
				if(educateInfo != null && educateInfo.size()>0){
					tEmpBaseInfo.setEduInfo(SysDictionaryUtil.getNameByValueForDefault("PersonnelQualifications",educateInfo.get(0).getEducName()));
				}
			}
            rs.setSuccess(true);
			rs.setData(tEmpBaseInfo);
			LOG.info("查找个人基本情况成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找个人基本情况失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 工作经历
	 */
	@RequestMapping("/getTWorkExpInfoForSenior")
	@ResponseBody
	public ResultData getTWorkExpInfoForSenior(String erpNo,String seniorId) {
		ResultData rs = new ResultData();
		try {
			List<WorkExpInfoVo> tWorkExpInfos = userDataInfoService.getWorkExpInfoByErpNo(erpNo,seniorId);
            rs.setSuccess(true);
			rs.setData(tWorkExpInfos);
			LOG.info("查找个人工作经历成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找个人工作经历失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 主要学习、培训经历
	 */
	@RequestMapping("/getStudyAndTrainForSenior")
	@ResponseBody
	public ResultData getStudyAndTrainForSenior(String erpNo,String seniorId) {
		ResultData rs = new ResultData();
		try {
			List<StudyAndTrainVo> educateInfos = userDataInfoService.getEducateInfoByErpNo(erpNo,seniorId);
			List<StudyAndTrainVo> contiStudyInfos = userDataInfoService.getContiStudyInfoByErpNo(erpNo,seniorId);
			educateInfos.addAll(contiStudyInfos);
            rs.setSuccess(true);
			rs.setData(educateInfos);
			LOG.info("查找个人学习、培训经历成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找个人学习、培训经历失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 技术革新、技术改造、科技成果转化、关键问题处理
	 * 即：技术革新、专利、科技创新、项目成果
	 */
	@RequestMapping("/getSkillAndTechnicalForSenior")
	@ResponseBody
	public ResultData getSkillAndTechnicalForSenior(String erpNo,String seniorId) {
		ResultData rs = new ResultData();
		try {
			//技艺革新
			List<SkillAndTechnicalVo> skillAndTechnicalVos = userDataInfoService.getTecInnovInfoByErpNo(erpNo,seniorId);
			//专利
			List<SkillAndTechnicalVo> patentInfos = userDataInfoService.getPatentInfoByErpNo(erpNo,seniorId);
			//科技创新
			List<SkillAndTechnicalVo> tecManInnovs = userDataInfoService.getTecManInnovByErpNo(erpNo,seniorId);
			//项目成果
			List<SkillAndTechnicalVo> projAchieveInfos = userDataInfoService.getProjAchieveInfoByErpNo(erpNo,seniorId);
//			skillAndTechnicalVos.addAll(patentInfos);
//			skillAndTechnicalVos.addAll(tecManInnovs);
//			skillAndTechnicalVos.addAll(projAchieveInfos);
			projAchieveInfos.addAll(tecManInnovs);
			projAchieveInfos.addAll(patentInfos);
			projAchieveInfos.addAll(skillAndTechnicalVos);
            rs.setSuccess(true);
			rs.setData(projAchieveInfos);
			LOG.info("查找个人学习、培训经历成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找个人学习、培训经历失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 编写操作规程、规范、标准、教案及发表论文、著作等情况
	 * 即：标准制定、学术成果
	 */
	@RequestMapping("/getStandAndScienceForSenior")
	@ResponseBody
	public ResultData getStandAndScienceForSenior(String erpNo,String seniorId) {
		ResultData rs = new ResultData();
		try {
			//标准制定
			List<StandAndScienceVo> standAndSciences = userDataInfoService.getStandInfoByErpNo(erpNo,seniorId);
			//学术成果
			List<StandAndScienceVo> acadeAchiveInfos = userDataInfoService.getAcadeAchiveInfoByErpNo(erpNo,seniorId);
//			standAndSciences.addAll(acadeAchiveInfos);
			acadeAchiveInfos.addAll(standAndSciences);
            rs.setSuccess(true);
			rs.setData(acadeAchiveInfos);
			LOG.info("查找个人编写操作规程、规范、标准、教案及发表论文、著作等情况成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找个人编写操作规程、规范、标准、教案及发表论文、著作等情况失败！");
			e.printStackTrace();
		}
		return rs;
	}

    /**
     * 参加技能竞赛获奖情况
     * 即：竞赛考试
     */
    @RequestMapping("/getTContestInfoForSenior")
    @ResponseBody
    public ResultData getTContestInfoForSenior(String erpNo,String seniorId) {
        ResultData rs = new ResultData();
        try {
            //竞赛考试
            List<ContestInfoVo> tContestInfos = userDataInfoService.getContestInfoByErpNo(erpNo,seniorId);
            rs.setSuccess(true);
            rs.setData(tContestInfos);
            LOG.info("查找个人参加技能竞赛获奖情况成功");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("查找个人参加技能竞赛获奖情况失败！");
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 承担技艺传授、技能培训工作情况
     * 即：导师带徒、培训授课授课部分
     */
    @RequestMapping("/getTeachArtForSenior")
    @ResponseBody
    public ResultData getTeachArtForSenior(String erpNo,String seniorId) {
        ResultData rs = new ResultData();
        try {
            //师带徒信息
            List<TeachArtVo> tSdtTutorPrentInfos = userDataInfoService.getTSdtTutorPrentInfoByErpNo(erpNo,seniorId);
            //培训授课
            List<TeachArtVo> contiStudyInfos = userDataInfoService.getContiStudyTechInfoByErpNo(erpNo,seniorId);
//            tSdtTutorPrentInfos.addAll(contiStudyInfos);
			contiStudyInfos.addAll(tSdtTutorPrentInfos);
            rs.setSuccess(true);
            rs.setData(contiStudyInfos);
            LOG.info("查找个人承担技艺传授、技能培训工作情况成功");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("查找个人承担技艺传授、技能培训工作情况失败！");
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 获得荣誉称号
     * 即：个人荣誉、专家人才称号、专业技术资格、技能等级
     */
    @RequestMapping("/getTHonorDatasForSenior")
    @ResponseBody
    public ResultData getTHonorDatasForSenior(String erpNo,String seniorId) {
        ResultData rs = new ResultData();
        try {
            //个人荣誉
            List<HonorDatasVo> honnorInfos = userDataInfoService.getHonnorInfoByErpNo(erpNo,seniorId);
            //专家人才称号
            List<HonorDatasVo> expertTitleInfos = userDataInfoService.getExpertTitleInfoByErpNo(erpNo,seniorId);
            //专业技术资格
            List<HonorDatasVo> proTecInfos = userDataInfoService.getProTecInfoByErpNo(erpNo,seniorId);
            //技能等级
            List<HonorDatasVo> skillLevelInfos = userDataInfoService.getSkillLevelInfoByErpNo(erpNo,seniorId);
//            honnorInfos.addAll(expertTitleInfos);
//            honnorInfos.addAll(proTecInfos);
//            honnorInfos.addAll(skillLevelInfos);
			skillLevelInfos.addAll(proTecInfos);
			skillLevelInfos.addAll(expertTitleInfos);
			skillLevelInfos.addAll(honnorInfos);
            rs.setSuccess(true);
            rs.setData(skillLevelInfos);
            LOG.info("查找个人荣誉称号成功");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("查找个人荣誉称号失败！");
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 操作可见或隐藏
     * @param id
     * @return
     */
    @RequestMapping("/handleHideOrShowForSenior")
    @ResponseBody
    public ResultData handleHideOrShowForSenior(String erpNo,String id,String isHide,String seniorId) {
        ResultData rs = new ResultData();
        try {
            if(StringUtil.isNullOrEmpty(id) || StringUtil.isNullOrEmpty(isHide)){
                rs.setSuccess(false);
                rs.setErrorMsg("参数错误");
                return rs;
            }
            tSeniorTechnicianHandleService.solveData(erpNo,id,isHide,seniorId);
            rs.setSuccess(true);
            rs.setData("操作成功");
            LOG.info("操作成功");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("操作失败！");
            e.printStackTrace();
        }
        return rs;
    }


	/**
	 * 技能信息导出到word
	 * @param response
	 */
	@GetMapping("/exportWord")
	public void exportWord(HttpServletRequest request,HttpServletResponse response,String seniorId){
		User user = this.getLoginUserInfo(request);
		try {
			if (StringUtil.isNullOrEmpty(user.getFax())){
				LOG.error("获取当前登录人人资编号失败");
				return;
			}
			LOG.info("技能等级申报导出word,查询数据开始");
			//获取待导出的数据
			TechnicalReportVo modelData = exportWordUserDataInfoService.getModelData(user.getFax(),seniorId);
			LOG.info("信息导出word,查询数据完成,开始导出word");
			//响应页面
			wordExportService.exportWord(response, modelData,user.getName()+"技能等级申报信息导出",user.getFax());
			LOG.info("个人信息导出word,导出word完成");
		}catch (Exception e){
			e.printStackTrace();
			LOG.error("个人信息导出word异常!!!",e);
		}

	}



	
}
